﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ListJob
{
    class Book : IComparable<Book>, IFormattable//继承两个接口
                                                //IComparable<T> 接口定义由值类型或类实现的通用比较方法，以为排序实例创建类型特定的比较方法。
                                                //IFormattable 接口 提供一种功能，用以将对象的值格式化为字符串表示形式。
    {
        private string BookName;
        private string Author;
        private string PublishDate;
        private string Publisher;
        private string ClassId;
        private string ClassName;
        private string ISBN;

        public Book(string bookName, string author, string publishDate, string publisher, string classId, string className, string iSBN)
        {
            BookName = bookName;//书名
            Author = author;//作者
            PublishDate = publishDate;//出版日期
            Publisher = publisher;//出版社
            ClassId = classId;//类别
            ClassName = className;//类别名称
            ISBN = iSBN;//ISBN
        }

        public string BookName1 { get => BookName; set => BookName = value; }
        public string Author1 { get => Author; set => Author = value; }
        public string PublishDate1 { get => PublishDate; set => PublishDate = value; }
        public string Publisher1 { get => Publisher; set => Publisher = value; }
        public string ClassId1 { get => ClassId; set => ClassId = value; }
        public string ClassName1 { get => ClassName; set => ClassName = value; }
        public string ISBN1 { get => ISBN; set => ISBN = value; }


        public override string ToString() => $"书名：{BookName} 作者：{Author} 出版日期：{PublishDate} 出版社：{Publisher}类别：{ClassId}类别名称：{ClassName}";

        public virtual string ToString(string format) => ToString(format, null);


        public string ToString(string format, IFormatProvider formatProvider)
        {
            switch (format)
            {
                case null:
                case "A":
                    return ToString();
                case "Id":
                    return $"类别：{ClassId}";
                case "Name":
                    return $"书名：{BookName}";
                case "NP":
                    return $"书名：{BookName} 作者：{Author}";
                case "H":
                    return $"出版社：{Publisher}";
                case "C": return $"书名：{BookName} 作者：{Author} 出版日期：{PublishDate} 出版社：{Publisher}类别：{ClassId}类别名称：{ClassName}";
                default:
                    throw new FormatException($"invalid format string {format}");
            }
        }

        public int CompareTo(Book other)
        {
            int compare = BookName?.CompareTo(other?.BookName) ?? -1;
            if (compare == 0)
            {
                return ISBN?.CompareTo(other?.ISBN) ?? -1;
            }
            return compare;
        }

    }
}
